import * as THREE from "../../three.module";
//fairyui 
import * as fgui from "fairygui-three"
import UI_login from "./fui/UI_login"
import { GLTFLoader } from "../framework/threeex/GLTFLoader";
import Scheduler from "../framework/inc/scheduler";

export default class AutoGoStage {
    constructor(sceneobj, camera) {
        this._scene = sceneobj
        this._camera = camera

        // remove robot in editor
        let mod_robot = this._scene.getObjectByName("mod_robot")
        if (mod_robot) {
            this._scene.remove(mod_robot)
        }

        /*  todo rescachemgr */
        fgui.UIPackage.loadPackage('res/f_ui/a_autogo').then(() => {
            this.initGUI()
            this.initStage()
        })
        /*  todo rescachemgr */

    }

    initGUI() {
        let view = new UI_login()
        fgui.GRoot.inst.addChild(view)
    }

    initStage() {
        this._ani_robot()
        this._crt_flybox()
    }

    _ani_robot() {
        this._animixer_robot = undefined
        // random clip animation 
        let gltfloader = new GLTFLoader()
        gltfloader.load("res/model/RobotExpressive.glb", (text) => {
            let ani_list = text.animations
            let mod_robot = text.scene
            this._scene.add(mod_robot)
            const update_ani = function () {
                let clip = ani_list[Math.floor(Math.random() * ani_list.length)]
                if (clip) {
                    mixer.clipAction(clip).play()
                }
            }
            // mixer
            let mixer = new THREE.AnimationMixer(mod_robot);
            mod_robot.position.x = -5
            update_ani()
            mixer.addEventListener('loop', (e) => {
                console.log("loop")
                mixer.stopAllAction()
                update_ani()
            })
            this._animixer_robot = mixer
        })

    }

    _crt_ground() {
    }

    _crt_flybox() {
        const SQUAR_NUM = 1000
        const geometry = new THREE.BoxBufferGeometry(20, 20, 20);
        for (let i = 0; i < SQUAR_NUM; i++) {
        }
    }


    update() {
        if (this._animixer_robot) {
            this._animixer_robot.update(Scheduler.getInstance().dt() / 2)
        }
    }
}